package cs341.group2.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;

import cs341.group2.mycontacts.Database.Category;
import cs341.group2.mycontacts.Database.Contact;
import cs341.group2.mycontacts.Database.MyContactsDB;
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	private MyContactsDB myContactsDb;
	private String currentCategoryName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myContactsDb = new MyContactsDB(this);
		
		Button a = (Button) findViewById(R.id.button_add);
		a.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainActivity.this,
						cs341.group2.mycontacts.ContactPage.class);
				i.putExtra("state", "add");
				startActivity(i);
			}
		});

		Button b = (Button) findViewById(R.id.button_search);
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainActivity.this,
						cs341.group2.mycontacts.Search.class);
				startActivity(i);
			}
		});

		Button c = (Button) findViewById(R.id.SubmitNewCategoryButton);
		c.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText text = (EditText) findViewById(R.id.NewCategoryNameField);
				currentCategoryName = text.getText().toString();
				
				new InsertNewCategory().execute(currentCategoryName);
				
				
			}
		});
	}
	
	public void goToCategoryPage(int categoryId){
		Intent intent = new Intent(MainActivity.this,
				cs341.group2.mycontacts.ViewCategory.class);
		intent.putExtra("Name", currentCategoryName);
		intent.putExtra("Id", categoryId);
		startActivity(intent);
	}

	@SuppressLint("NewApi")
	public void populateCategoryButtons(final Category[] categories) {
		ListView list = (ListView)findViewById(R.id.CategoriesListView);
		list.setClickable(true);
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		for(int i = 0; i < categories.length; i++){
	        map = new HashMap<String, String>();
	        map.put("categoryName", categories[i].Name);
	        map.put("categoryId", String.valueOf(categories[i].Id));
	        listItem.add(map);
		}
		CategoryButtonAdapter adapter = new CategoryButtonAdapter (this.getBaseContext(), listItem);
		list.setAdapter(adapter);
	}

	@Override
	public void onResume() {
		super.onResume(); // Always call the superclass method first

		new PopulateAllCategories().execute((Object[]) null);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private class PopulateAllCategories extends
			AsyncTask<Object, Object, Category[]> {
		public MyContactsDB myContactsDb = new MyContactsDB(MainActivity.this);

		@Override
		protected Category[] doInBackground(Object... params) {
			myContactsDb.open();
			return myContactsDb.getAllCategories();
		}

		@Override
		protected void onPostExecute(Category[] categories) {
			myContactsDb.close();
			populateCategoryButtons(categories);
		}
	}
			
	private class InsertNewCategory extends AsyncTask<String, Object, Integer> {
		public MyContactsDB myContactsDb = new MyContactsDB(MainActivity.this);

		protected Integer doInBackground(String... params) {
			// TODO Auto-generated method stub
			myContactsDb.open();
			return myContactsDb.insertCategory(params[0]);
		}
		
		@Override
		protected void onPostExecute(Integer id) {
			myContactsDb.close();
			goToCategoryPage(id);
		}
	}

}
